<?php
/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     block
 * Name:     pmvc_hasRoles
 * Purpose:  disables\enables blocks based on user roles
 * -------------------------------------------------------------
 */

use pmvc\template\smarty\SmartyUtil;
use pmvc\mvc\security\SecurityUtil;

function smarty_block_pmvc_hasRoles($params, $content, &$smarty, &$repeat) {

	SmartyUtil::assertRequiredParameters($params, Array(
		"roles"
	));

	// get the request object
    $request = SmartyUtil::getRegisteredObject($smarty, "Request", true);

    // get the roles
    $roles = $params["roles"];
    if (!is_array($roles)) {
    	$roles = explode(",", $roles);
    }

    // get the user
    $user = SecurityUtil::getCurrentUser($request);
    if (!isset($user)) {
    	return "";
    }

    // do they have to be missing all of them?
    $all = isset($params["all"]) ? $params["all"]===true : false;

    // show content
    $showContent = ($all)
    	? SecurityUtil::hasAllRoles($request, $roles)
    	: SecurityUtil::hasAnyRoles($request, $roles);
    if ($showContent) {
    	return $content;
    }
}

?>